package cn.com.victorysoft.ylgl.service.fracturingpressure.controller.jobTest;

import cn.com.victorysoft.ylgl.service.commons.mybatis.paging.bean.DataPaging;
import cn.com.victorysoft.ylgl.service.commons.web.JsonMessage;
import cn.com.victorysoft.ylgl.service.commons.web.RequestContext;
import cn.com.victorysoft.ylgl.service.commons.web.User;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.jobTest.InjectionProfile;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.jobTest.InjectionProfileVO;
import cn.com.victorysoft.ylgl.service.fracturingpressure.service.jobTest.IInjectionProfileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 吸水剖面
 * Created by Administrator on 18/3/16.
 */
@Controller
@RequestMapping({"jobTest/injectionProfile"})
public class InjectionProfileController {
        private static final Logger logger = LoggerFactory.getLogger(InjectionProfileController.class);

        @Resource
        private IInjectionProfileService service;

        public InjectionProfileController() {
        }

        @RequestMapping(
                value = {"/list.wet"},
                method = {RequestMethod.GET, RequestMethod.POST}
        )
        public String list() {
            return "jobTest/injectionProfile/list";
        }

        @RequestMapping(
                value = {"/get.wds"},
                method = {RequestMethod.POST,RequestMethod.GET}
        )
        @ResponseBody
        public JsonMessage get(@RequestParam(value="id", required=true) String id) {
            InjectionProfile entity = this.service.getById(id);
            return (new JsonMessage()).success(entity);
        }

        @RequestMapping(
                value = {"/add.wds"},
                method = {RequestMethod.POST}
        )
        @ResponseBody
        public JsonMessage createEntity(@ModelAttribute("model") InjectionProfileVO entity) {
            User user = RequestContext.getCurrentUser();
            entity.setCreateUserId(user.getName());
            entity.setCreateDate(new Date());
            this.service.insert(entity);
            InjectionProfileVO newEntity = service.getDtoBy(entity.getDataId());
            return (new JsonMessage()).success(newEntity);
        }


        @RequestMapping(
                value = {"/update.wds"},
                method = {RequestMethod.POST}
        )
        @ResponseBody
        public JsonMessage updateEntity(@ModelAttribute("model") InjectionProfileVO entity) {
            //先根据faid查以前方案的信息
            InjectionProfile oldEntity = service.getById(entity.getDataId());
            //用户需要历史数据

            User user = RequestContext.getCurrentUser();
            entity.setUpdateUserId(user.getName());
            entity.setUpdateDate(new Date());

            this.service.updateByPrimaryKey(entity);
            InjectionProfileVO newEntity = service.getDtoBy(entity.getDataId());
            return (new JsonMessage()).success(newEntity);
        }


        /**
         * @param ids:1;2;3;
         * @return
         */
        @RequestMapping(value={"/delete.wds"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}, produces={"application/json;charset=UTF-8"})
        @ResponseBody
        public JsonMessage delete(@RequestParam(value="ids", required=true) String ids)
        {
            try
            {
                this.service.deleteByKeys(ids);
                return new JsonMessage().success();
            }
            catch (Exception e)
            {
                logger.error("删除出错: {}", e);
            }
            return new JsonMessage().failure("失败");
        }

        @RequestMapping(
                value = {"/queryPagingList.wds"},
                method = {RequestMethod.GET, RequestMethod.POST}
        )
        @ResponseBody
        public JsonMessage queryPagingList(@RequestParam(value = "page",required = false) String page,
                                           @RequestParam(value = "rows",required = false) String rows,
                                           @RequestParam(value = "yt",required = false) String yt,
                                           @RequestParam(value = "qk",required = false) String qk,
                                           @RequestParam(value = "dk",required = false) String dk,
                                           @RequestParam(value = "cw",required = false) String cw,
                                           @RequestParam(value = "jh",required = false) String jh,
                                           @RequestParam(value = "startDate",required = false) String startDate,
                                           @RequestParam(value = "endDate",required = false) String endDate) {

            DataPaging<InjectionProfileVO> dataPaging =  this.service.queryPagingList(yt,qk,dk,cw,jh,startDate,endDate, rows,page);
            return (new JsonMessage()).success(dataPaging);
        }

    }
